package controllers;

import beans.InhoudBean1;
import models.DigitalClass;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import models.Course;
import models.Module;
import tools.Checker;

public class PlanDigitalClassController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        HttpSession sessie = request.getSession();
        Module module = new Module();
        int moduleID;
        moduleID = Integer.parseInt(request.getParameter("id"));
        module.setModuleID(moduleID);
        sessie.setAttribute("currentModule", module);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/planDigitalClass.jsp");
        dispatcher.forward(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        String description = "", address = "", startTime = "", endTime = "", date = "", timeZone = "";
        int maxStudents = 0, moduleID;
        boolean isEmpty = false;
        HttpSession sessie = request.getSession();
        Module module = new Module();
        InhoudBean1 inhoud = (InhoudBean1) sessie.getAttribute("inhoud");

        if (!request.getParameter("date").isEmpty()) {
            date = request.getParameter("date");
            request.setAttribute("date", date);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError14());
        }
        if (!request.getParameter("startTime").isEmpty()) {
            startTime = request.getParameter("startTime");
            request.setAttribute("startTime", startTime);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError15());
        }
        if (!request.getParameter("endTime").isEmpty()) {
            endTime = request.getParameter("endTime");
            request.setAttribute("endTime", endTime);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError16());
        }
        if (!request.getParameter("timeZone").isEmpty()) {
            timeZone = request.getParameter("timeZone");
            request.setAttribute("timeZone", timeZone);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError17());
        }
        if (!request.getParameter("maxStudents").isEmpty()) {
            maxStudents = Integer.parseInt(request.getParameter("maxStudents"));
            request.setAttribute("maxStudents", maxStudents);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError18());
        }
        if (!request.getParameter("description").isEmpty()) {
            description = request.getParameter("description");
            request.setAttribute("description", description);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError2());
        }

        module = (Module) sessie.getAttribute("currentModule");
        if (!isEmpty) {
            DigitalClass digitalClass = new DigitalClass(startTime, endTime, timeZone, date, description, module.getModuleID(), maxStudents);
            queryManager.planDigitalClass(digitalClass);
            address = "/leraar_home.jsp";
        } else {
            address = "/planDigitalClass.jsp";
        }
        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);

        dispatcher.forward(request, response);
    }
}
